package com.elm.util;

import java.util.InputMismatchException;
import java.util.Scanner;

public class ConsoleUtil {

    private static final Scanner input = new Scanner(System.in);

    // ANSI Color Codes
    public static final String RESET = "\u001B[0m";
    public static final String BLACK = "\u001B[30m";
    public static final String RED = "\u001B[31m";
    public static final String GREEN = "\u001B[32m";
    public static final String YELLOW = "\u001B[33m";
    public static final String BLUE = "\u001B[34m";
    public static final String PURPLE = "\u001B[35m";
    public static final String CYAN = "\u001B[36m";
    public static final String WHITE = "\u001B[37m";

    // Box drawing characters
    private static final String H_LINE = "─";
    private static final String V_LINE = "│";
    private static final String TOP_LEFT = "┌";

    private static final String BOTTOM_LEFT = "└";



    private static final String T_LEFT = "├";


    private static final int BOX_WIDTH = 50; // Adjusted width for better visual

    // Custom repeat method for Java 8 compatibility
    private static String repeatString(String s, int count) {
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < count; i++) {
            sb.append(s);
        }
        return sb.toString();
    }

    // Adjusted centerString to only center, not pad to full width
    private static String centerString(String s) {
        int padding = (BOX_WIDTH - s.length()) / 2;
        return repeatString(" ", padding) + s + repeatString(" ", BOX_WIDTH - s.length() - padding);
    }

    public static void printBoxedMessage(String... messages) {
        System.out.println(TOP_LEFT + repeatString(H_LINE, BOX_WIDTH));
        for (String msg : messages) {
            System.out.println(V_LINE + centerString(msg));
        }
        System.out.println(BOTTOM_LEFT + repeatString(H_LINE, BOX_WIDTH));
    }

    public static void printBoxedMenu(String header, String prompt, String color, String... options) {
        System.out.println(TOP_LEFT + repeatString(H_LINE, BOX_WIDTH));
        System.out.println(V_LINE + centerString(header));
        System.out.println(T_LEFT + repeatString(H_LINE, BOX_WIDTH));
        System.out.println(V_LINE + String.format("%-" + BOX_WIDTH + "s", " " + prompt)); // Prompt is left-aligned with 1 space
        System.out.println(V_LINE + repeatString(" ", BOX_WIDTH)); // Empty line for spacing

        // Options in two columns
        for (int i = 0; i < options.length; i += 2) {
            String option1 = (i + 1) + ". " + options[i];
            String option2 = (i + 2 <= options.length) ? ((i + 2) + ". " + options[i + 1]) : "";

            // Calculate padding for two columns
            // Each column takes roughly half the width, minus some separator space
            int colWidth = (BOX_WIDTH - 3) / 2; // 3 for "   " separator

            String line = "  " + String.format("%-" + colWidth + "s", option1);
            if (!option2.isEmpty()) {
                line += "   " + String.format("%-" + colWidth + "s", option2);
            }
            // Pad the rest of the line to fill BOX_WIDTH
            line = String.format("%-" + BOX_WIDTH + "s", line);
            System.out.println(V_LINE + line);
        }
        System.out.println(V_LINE + repeatString(" ", BOX_WIDTH)); // Empty line for spacing
        System.out.println(BOTTOM_LEFT + repeatString(H_LINE, BOX_WIDTH));
        System.out.print(color + "输入选项: " + RESET);
    }

    public static void printDivider(String title) {
        System.out.println(TOP_LEFT + repeatString(H_LINE, BOX_WIDTH));
        System.out.println(V_LINE + centerString(title));
        System.out.println(BOTTOM_LEFT + repeatString(H_LINE, BOX_WIDTH));
    }

    public static void printMenu(String header, String... options) {
        // This method will now delegate to the new printBoxedMenu
        // The prompt will be hardcoded for simplicity, as it's consistent
        printBoxedMenu(header, "> 请选择操作:", BLUE, options);
    }

    public static int readInt() {
        while (true) {
            try {
                int value = input.nextInt();
                input.nextLine(); // Consume newline left-over
                return value;
            } catch (InputMismatchException e) {
                System.out.println(RED + "输入无效，请输入一个整数。" + RESET);
                input.nextLine(); // Consume the invalid input
                System.out.print(BLUE + "请重新输入: " + RESET);
            }
        }
    }

    public static String readString() {
        return input.nextLine();
    }

    public static void printMessage(String message) {
        System.out.println(message);
    }

    public static void printSuccess(String message) {
        System.out.println(GREEN + "[成功] " + message + RESET);
    }

    public static void printError(String message) {
        System.err.println(RED + "[错误] " + message + RESET);
    }
}
